package com.iflytek.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iflytek.entity.Order;
import com.iflytek.entity.User;
import com.iflytek.service.OrderService;
import com.iflytek.service.UserService;
import com.iflytek.vo.UploadMsgDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.UUID;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;
    @ResponseBody
    @GetMapping("/submitOrder")
    public String submitOrder(String id, Integer cartId){
        if (!id.trim().equals("undefined")) {
            User user = userService.getById(id);
            //把用户新注入订单中
            Order order = new Order();
            BeanUtils.copyProperties(user, order);
            //随机生产订单号
            System.out.println(UUID.randomUUID().toString());
            order.setOrderId(UUID.randomUUID().toString().replaceAll("-", ""));
            //绑定车辆id
            order.setCartId(cartId);
            orderService.save(order);
        } else
            return "请选择用户";
        return "添加订单成功";
    }

    //添加租车人
    @ResponseBody
    @PostMapping("/addUser")
    public Object addUser(UploadMsgDTO msgDTO) {
        User user = new User();
        BeanUtils.copyProperties(msgDTO, user);
        QueryWrapper<User> w = new QueryWrapper<>();
        w.eq("id_card",user.getIdCard());
        if (userService.getOne(w)==null){
            userService.save(user);
            return user;
        }else return "用户已存在";
    }

}
